﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;

namespace DAL
{
    public class OutNewListOnDataAccess : FreshCmsAccessBase
    {
        #region code CMS

        public int InsertOutNewListOn(OutNewListOn OutNewListOn)
        {
            try
            {
                const string spName = "spi_OutNewListOn_Insert";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@IsParent", OutNewListOn.IsParent),
										new SqlParameter("@CatId", OutNewListOn.CatId),
										new SqlParameter("@NewId", OutNewListOn.NewId),
										new SqlParameter("@ListonId", OutNewListOn.ListonId),
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ListonId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ListonId"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@ListonId"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int DeleteOutNewListOn(int newId)
        {
            try
            {
                const string spName = "spi_OutNewListOn_DeleteByNewId";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@NewId", newId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public List<OutNewListOn> GetOutNewListOnListByNewId(int newId)
        {
            try
            {
                const string spName = "spi_OutNewListOn_GetByNewId";

                var sqlParams = new[]
                                    {
										new SqlParameter("@NewId", newId)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var OutNewListOnList = DbHelper.ExecuteSqlList<OutNewListOn>(sqlCommand);
                return OutNewListOnList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<OutNewListOn>();
            }
        }

        #endregion
    }
}
